package com.wowudi.service.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.corba.se.spi.orbutil.threadpool.WorkQueue;
import com.wowudi.entity.Role;
import com.wowudi.entity.dto.Result;
import com.wowudi.mapper.RoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wowudi.mapper.UserMapper;
import com.wowudi.entity.User;
import com.wowudi.service.UserService;
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService{

    @Autowired
    private UserMapper userMapper;

    @Autowired
    private RoleMapper roleMapper;

    /**
    * Description: 登录<br/>
    * date: 2022-3-14<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result login(String name, String pwd) {
        // 根据用账号和密码查询用户信息
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("user_name",name).eq("password",pwd);
        User user = userMapper.selectOne(wrapper);

        Result result = new Result();

        HashMap<String, Object> hashMap = new HashMap<>();
        if (user != null){
            // 获取该用户的角色
            List<Role> roleList = roleMapper.selectRoleByUserId(user.getUserId());
            List<String> roles = new ArrayList<>();
            roleList.forEach(role -> {
                roles.add(role.getRoleName());
            });
            hashMap.put("user",user);
            hashMap.put("roles",roles);
        }else{
            hashMap.put("user",null);
            hashMap.put("roles",null);
            result.setCode(500);
        }
        result.setData(hashMap);

        return result;
    }



    /**
    * Description: 修改用户信息<br/>
    * date: 2022-3-14<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result updateUser(User user) {
        int i = userMapper.updateById(user);
        Result result = new Result();
        result.setMessage(i==0?"操作繁忙，请稍后再试！":"修改成功！");
        return result;
    }

    /**
    * Description: 查询所有用户信息<br/>
    * date: 2022-3-15<br/>
    * @author 贺同志<br/>
    */
    @Override
    public Result findPageUser(String userName) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.like("user_name",userName);

        Page<User> page = new Page<>(1,3);
        Page<User> pageUser = userMapper.selectPage(page, wrapper);

        Result result = new Result();
        if (pageUser != null){
            result.setData(pageUser);
            result.setMessage("查询成功!");
        }else{
            result.setMessage("操作繁忙，请稍后再试！!");
        }
        return result;
    }
}
